package com.penghaisoft.microwms.product.web.controller.expose;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.ws.rs.core.MediaType;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.penghaisoft.microwms.common.Constant;
import com.penghaisoft.microwms.common.controller.BaseController;
import com.penghaisoft.microwms.common.dto.ConProCondDto;
import com.penghaisoft.microwms.common.dto.TransResult;
import com.penghaisoft.microwms.common.page.Pager;
import com.penghaisoft.microwms.common.service.IMesInterfaceService;
import com.penghaisoft.microwms.common.service.MesProperties;
import com.penghaisoft.microwms.common.wms.model.MesConProRequired;
import com.penghaisoft.microwms.common.wms.model.ProContractChangePlanSn;
import com.penghaisoft.microwms.common.wms.model.ProStorageSn;
import com.penghaisoft.microwms.product.service.IProContractChangePlanSnService;
import com.penghaisoft.microwms.product.service.IProStorageSnService;

import lombok.extern.slf4j.Slf4j;

/** 
 * @Package com.penghaisoft.microwms.product.web.controller.expose
 * @ClassName: ExposeMesPhoneController
 * @Description: 成品手机接口
 * @author luoteng
 * @date 2019年3月11日 上午10:55:49
 */
@RestController
@RequestMapping(value = "expose/mes/phone")
@Slf4j
public class ExposeMesPhoneController extends BaseController  {
	
	@Autowired
	private IProStorageSnService proStorageSnService;
	@Autowired
	private IMesInterfaceService mesInterfaceService;
	
	@Autowired
	private IProContractChangePlanSnService iProContractChangePlanSnService;
	
	@Resource
	private MesProperties mesProperties;
	
	@PostMapping("storage")
	public TransResult proStorage(@RequestBody ProStorageSn proStorageSn) {
		TransResult result = new TransResult();
		
		if(proStorageSn.getContractNo()==null || proStorageSn.getContractNo().equals("")){
			result.setCode(Constant.FAIL);
			result.setMsg("请输入合同号");
			return result;
		}
		
		MesConProRequired mesConProRequired = new MesConProRequired();
		mesConProRequired.setSalesCode(proStorageSn.getContractNo());
		
		String url =  mesProperties.getUrl() + "/imes-provider/external/interfaces/contract/product";
		JSONObject handResp = mesInterfaceService.publicWSJson(url, MediaType.APPLICATION_JSON_TYPE, JSON.toJSONString(mesConProRequired));
		JSONArray listjson = handResp.getJSONObject("data").getJSONArray("list");
		
		ConProCondDto conProCondDto = new ConProCondDto();
		conProCondDto.setContractNo(proStorageSn.getContractNo());
		List<MesConProRequired> list = proStorageSnService.findConProRequired(conProCondDto);
		
		List<MesConProRequired> mesList = new ArrayList<MesConProRequired>();
		if(list.size()>0){
			for(int i=0; i<list.size();i++){
				MesConProRequired conProRequired = new MesConProRequired();
				conProRequired.setContractNo(list.get(i).getContractNo());
				conProRequired.setModel(list.get(i).getModel());
				conProRequired.setInAmount(list.get(i).getInAmount());
				conProRequired.setOutAmount(list.get(i).getOutAmount());
				conProRequired.setKcAmount(list.get(i).getKcAmount());
				conProRequired.setJcAmount(list.get(i).getJcAmount());
				conProRequired.setJrAmount(list.get(i).getJrAmount());
				conProRequired.setUnit(list.get(i).getUnit());
				
				for(int j=0; j<listjson.size();j++){
					JSONObject temp = listjson.getJSONObject(j);
					if(list.get(i).getModel().equals(temp.getString("materialCode"))){
						conProRequired.setDemand(new BigDecimal (temp.getString("demand")));
//						conProRequired.setUnit(temp.getString("measureUnit"));
//						conProRequired.setOwner(temp.getString("owner"));
						break;
					}
				}
				
				mesList.add(conProRequired);
			}
		}
		
		return success(mesList);
	}
	
	/**
	 * @Title: jy
	 * @Description: 原材料合同借用汇总详情
	 * @date 2019年3月12日 下午4:32:31
	 * @author luoteng
	 * @param proContractChangePlanSn
	 * @return TransResult   
	 */
	@PostMapping("jy")
	public TransResult jy(@RequestBody ProContractChangePlanSn proContractChangePlanSn) {
		TransResult result = new TransResult();
		
		if(proContractChangePlanSn.getContractNoOld()==null || proContractChangePlanSn.getContractNoOld().equals("")){
			result.setCode(Constant.FAIL);
			result.setMsg("请输入合同号");
			return result;
		}
		
		List<ProContractChangePlanSn> list = iProContractChangePlanSnService.getJYHZ(proContractChangePlanSn);
		
		return success(list);
	}
	
	/**
	 * @Title: jyInfo
	 * @Description: 原材料合同借用明细
	 * @date 2019年3月13日 下午1:27:12
	 * @author luoteng
	 * @param proContractChangePlanSn
	 * @return TransResult   
	 */
	@PostMapping("jy/info")
	public TransResult jyInfo(@RequestBody ProContractChangePlanSn proContractChangePlanSn) {
		TransResult result = new TransResult();
		
		if(proContractChangePlanSn.getContractNoOld()==null || proContractChangePlanSn.getContractNoOld().equals("")){
			result.setCode(Constant.FAIL);
			result.setMsg("请输入合同号");
			return result;
		}
		
		Pager<ProContractChangePlanSn> list = iProContractChangePlanSnService.getJYHZInfo(proContractChangePlanSn.getPage(), proContractChangePlanSn.getRows(), proContractChangePlanSn);
		
		return success(list);
	}
}
